from random import randrange

def randsym():
    if randrange(2) == 0:
        return '+'
    return '*'

def spaces(count):
	return ' ' * count

def gen_rand(depth, maxnum, length):		
    def gen_aux(depth):
        if depth == 0:
            return str(randrange(maxnum))
        else:
            cur = randrange(length)
            s = gen_aux(depth-1)
            for i in range(cur):
                s += randsym() + gen_aux(depth-1)
            return '(' + s + ')'
    return gen_aux(depth)
		
def gen(depth, maxnum, length):
    def gen_aux(depth):
        if depth == 0:
            return str(randrange(maxnum))
        else:
            cur = length-1
            s = gen_aux(depth-1)
            for i in range(cur):
                s += randsym() + gen_aux(depth-1)
            return '(' + s + ')'
    return gen_aux(depth)
	
def gen_with_spaces(depth, maxnum, length, space_count):
    def gen_aux(depth):
        if depth == 0:
            return str(randrange(maxnum))
        else:
            cur = length-1
            s = gen_aux(depth-1)
            for i in range(cur):
                s += spaces(space_count) + randsym() + spaces(space_count) + gen_aux(depth-1)
            return '(' + spaces(space_count) + s + spaces(space_count) + ')'
    return gen_aux(depth)

def main():
    p = gen(7, 300, 10)
    with open('expr.txt', 'w') as f:
        f.write(p)

if __name__ == '__main__':
    main()
